package com.navinfo.tripanalysis.service.impl;

import com.navinfo.tripanalysis.common.arithmetic.common.DrivingData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterEventData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.pojo.AlgorithmChainCommand;
import com.navinfo.tripanalysis.pojo.Point0200;
import com.navinfo.tripanalysis.pojo.Point;
import com.navinfo.tripanalysis.service.AbstractAlgorithmChainService;
import com.navinfo.tripanalysis.util.IntegrateSupportUtil;

import java.util.*;

/**
 * 0f37算法链调用，并输出行程及事件
 * @author wangshuai
 */
public class AlgorithmChainService0F37 extends AbstractAlgorithmChainService {
    /**
     * 停车通电，0200发动机转速条件
    *  private int rotation0200 = 300;
     */
    public AlgorithmChainService0F37(Map algorithmConfig) {
        super(algorithmConfig);
    }

    @Override
    protected OuterStatisticData collectTripData(DrivingData drivingData, long tid, Map<Long, Integer> tileLcMapping) {
        OuterStatisticData statisticData = super.collectTripData(drivingData, tid, tileLcMapping);
        statisticData.setStype(OuterStatisticData.S_TYPE_0F37);
        return statisticData;
    }

    @Override
    protected void collectEventData(AlgorithmChainCommand command, long tid, OuterEventData eventData) {
        super.collectEventData(command, tid, eventData);
        eventData.setStype(OuterEventData.S_TYPE_0F37);
    }

    @Override
    public void execute(AlgorithmChainCommand command) {
        List<Point> points = command.getPoints0f37();

        //0f37与0200点进行混流后输出
        if (null != command.getPoints0200()) {
            points = new ArrayList<>(command.getPoints0f37().size() + command.getPoints0200().size());
            points.addAll(command.getPoints0200());
            points.addAll(command.getPoints0f37());
            Collections.sort(points, Point.COMPARATOR);
        }

        if (null !=points && points.size() > 0) {
            int pointSize = points.size();

            //最后一个0f37点
            int last0f37Index = -1;
            for (int i=pointSize-1; i>=0; i--) {
                if (Point.PROTOCOL_0F37 == points.get(i).getProtocol() ) {
                    last0f37Index = i;
                    break;
                }
            }
            if (last0f37Index == -1) {
                logger.error("未发现0f37点，不处理，{}", command.getTid());
                return;
            }

            DrivingData drivingData = command.getDrivingData();
            for (int i=0; i<pointSize; i++) {
                Point point = points.get(i);
                //若原始点是0F37
                switch (point.getProtocol()){
                    //若原始点是0F37
                    case Point.PROTOCOL_0F37:
                        //如果是最后一个0f37点
                        if (i == last0f37Index) {
                            dealLastPoint(command, drivingData, point);
                            break;
                        }else{
                            dealPoint(command, drivingData, point);
                        }
                        break;
                    //若原始点是0200
                    case Point.PROTOCOL_0200:
                        //复制0200的数据到算法的公共变量中
                        IntegrateSupportUtil.assign0200Data(drivingData ,(Point0200) point);
                        break;
                    default:
                        break;
                }
            }

            if(command.isHandleCrossDay()) {
                handleCrossDay(command.getOuterData().getOutStatistics(), drivingData);
            }
            command.getOuterData().setDrivingData(drivingData);
        } else {
            logger.error("原始点数据为空！");
        }
    }

    /**
     * 添加0200数据，累加停车通电时长
     * 累加停车通电时长只适用于流计算，离线计算中没有行程外的位置点
     * @param drivingData 中间变量
     * @param point       点信息
     */
    /*private void deal0200(DrivingData drivingData, Point0200 point) {
        //将0200相关指标放到drivingData的commonData中
        IntegrateSupportUtil.assign0200Data(drivingData ,point);

        //varTrip为null是行程外
        if (null == drivingData.getTripInfo()) {
            String objJson = null;
            try {
                objJson = JsonUtils.toJson(drivingData.getOuterStatistic().get(ParkingPowerOn.class.getSimpleName()));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            //累加停车通电时长
            if (objJson != null) {
                Long gpsTime = point.getGpsTime();
                VarParkingPowerOn varParkingPowerOn = JsonUtils.fromJson(objJson, VarParkingPowerOn.class);

                //0200的GPs时间大于上次行程结束时间
                if (null!=varParkingPowerOn  && gpsTime>varParkingPowerOn.getParkingTime()) {
                    //ACC开且上次ACC也为开
                    if (point.getAccStatus()==1 && varParkingPowerOn.isLastAccStatus() && point.getRotation() < rotation0200) {
                        //时间差大于0，停车通电时长累加
                        long time = gpsTime - varParkingPowerOn.getLastGpsTime();
                        if (time > 0) {
                            varParkingPowerOn.setParkingPowerOnTime((int) (varParkingPowerOn.getParkingPowerOnTime() + time));
                        }
                    }
                    //赋值
                    varParkingPowerOn.setLastGpsTime(gpsTime);
                    varParkingPowerOn.setLastAccStatus(point.getAccStatus()==1 && point.getRotation()<rotation0200);
                }
                drivingData.getOuterStatistic().put(ParkingPowerOn.class.getSimpleName(), varParkingPowerOn);
            }
        }
    }*/
}

